/*
 * @Author: 葛文杰
 * @Date: 2021/3/9 09:31
 * @Description: egg service 数据源字段配置
 */

const Service = require('egg').Service;
const DataSourceFieldProxy = require('../proxy/dataSourceFieldProxy');
const EnumerationManageProxy = require('../proxy/enumerationManageProxy');
const { buildTableData, getEnumerationInfoByIdAndType } = require('../util/utils');
const {
    ENUMERATION_NAME_FIELD,
} = require('../util/common');
const {
    SHOW_FILED,
} = require('../util/enumerationCode');

/**
 * 构造数据源字段列表
 */
async function constructorDataSourceFieldList(conn, dataSourceFieldList) {
    let dataSourceFieldArray = [];
    for (let dataSourceFieldInfo of dataSourceFieldList) {
        dataSourceFieldInfo = await constructorDataSourceConfigInfo(conn, dataSourceFieldInfo);
        dataSourceFieldArray.push(dataSourceFieldInfo);
    }
    return dataSourceFieldArray;
}

/**
 * 构造数据源配置
 */
async function constructorDataSourceConfigInfo(conn, dataSourceFieldInfo) {
    let { fieldUseType, fieldType } = dataSourceFieldInfo;
    dataSourceFieldInfo.fieldUseTypeLabel =
      await getEnumerationInfoByIdAndType(conn, fieldUseType, ENUMERATION_NAME_FIELD);
    dataSourceFieldInfo.fieldTypeLabel =
      await getEnumerationInfoByIdAndType(conn, fieldType, ENUMERATION_NAME_FIELD);
    return dataSourceFieldInfo;
}

class DataSourceFieldInfo extends Service {

    /**
     * 获取数据源字段信息列表
     */
    async getFieldListByDataSourceConfigId(dataSourceFieldCondition) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let { pageNo, pageSize, ...otherCondition } = dataSourceFieldCondition;
            otherCondition.pageNo = parseInt(pageNo);
            otherCondition.pageSize = parseInt(pageSize);
            let dataSourceFieldList = await DataSourceFieldProxy.getFieldListByDataSourceConfigId(conn, otherCondition);
            let total = await DataSourceFieldProxy.getFieldTotalByDataSourceConfigId(conn, otherCondition);
            dataSourceFieldList = await constructorDataSourceFieldList(conn, dataSourceFieldList);
            await conn.commit(); // 提交事务
            return buildTableData(dataSourceFieldList, total, pageNo, pageSize);
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 获取数据源字段信息通过id
     */
    async getFieldInfoById(dataSourceFieldId) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            const dataSourceFieldInfo = await DataSourceFieldProxy.getFieldInfoById(conn, dataSourceFieldId);
            await conn.commit(); // 提交事务
            return dataSourceFieldInfo;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 获取显示字段数组
     * @return {Promise<void>}
     */
    async getShowFieldArrayByType(dataSourceFieldId) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let enumerationCondition = {
                enumerationValue: SHOW_FILED
            };
            let { id: fieldUseType } =
              await EnumerationManageProxy.getEnumerationInfoByValue(conn, enumerationCondition);
            let condition = {
                dataSourceFieldId,
                fieldUseType
            };
            const dataSourceShowFieldArray =
              await DataSourceFieldProxy.getShowFieldArrayByType(conn, condition);
            await conn.commit(); // 提交事务
            return dataSourceShowFieldArray;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 保存数据源字段信息
     * @param dataSourceFieldCondition
     * @return {Promise<void>}
     */
    async insertDataSourceFieldInfo(dataSourceFieldCondition) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            const result = await DataSourceFieldProxy.insertDataSourceFieldInfo(conn, dataSourceFieldCondition);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 保存数据源字段信息
     * @param dataSourceFieldId 数据源id
     * @param fieldUseType 字段使用类型
     * @param fieldName 字段名称
     * @param fieldAnotherName 字段别名
     * @param fieldType 字段类型
     * @param fieldLength 字段长度
     * @return {Promise<void>}
     */
    async updateDataSourceFieldInfoById(
      dataSourceFieldId,
      fieldUseType,
      fieldName,
      fieldAnotherName,
      fieldType,
      fieldLength
    ) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let dataSourceFieldInfo = {
                dataSourceFieldId,
                fieldUseType,
                fieldName,
                fieldAnotherName,
                fieldType,
                fieldLength
            };
            const result = await DataSourceFieldProxy.updateDataSourceFieldInfoById(conn, dataSourceFieldInfo);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }

    /**
     * 删除数据源字段信息
     * @return {Promise<void>}
     */
    async deleteDataSourceFieldInfoById(dataSourceFieldId) {
        const conn = await this.app.mysql.beginTransaction();
        try {
            let condition = {
                id: dataSourceFieldId,
            };
            const result = await DataSourceFieldProxy.deleteFieldInfoById(conn, condition);
            await conn.commit(); // 提交事务
            return result;
        } catch (err) {
            await conn.rollback(); // 一定记得捕获异常后回滚事务！！
            throw err;
        }
    }
}

module.exports = DataSourceFieldInfo;


